package com.glasses.business.fragment;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.glasses.business.R;
import com.glasses.business.adapter.FilterAdapter;
import com.glasses.business.adapter.TagAdapter;
import com.glasses.business.appliction.MyApplication;
import com.glasses.business.base.AppBaseFragment;
import com.glasses.business.entity.SelectItem;
import com.glasses.business.event.BussEvent;
import com.glasses.business.http.NetUtils;
import com.glasses.business.utils.Tools;

import net.neiquan.applibrary.flowtag.FlowTagLayout;
import net.neiquan.applibrary.flowtag.OnTagSelectListener;
import net.neiquan.okhttp.NetCallBack;
import net.neiquan.okhttp.ResultModel;

import org.haitao.common.utils.AppLog;
import org.haitao.common.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;

/**
 * Author：邓勇军
 * English Name：Deng,The Sky Fucker!
 * Email：dandycoder@126.com
 * Date：2017/3/9
 * Version：1.0
 */

public class GlassFrameFilterFragment extends AppBaseFragment {
    @InjectView(R.id.filterRecylerView)
    RecyclerView filterRecylerView;
    @InjectView(R.id.filterReset)
    TextView filterReset;
    @InjectView(R.id.filterSure)
    TextView filterSure;
    private FilterAdapter adapter;
    private List<SelectItem> list;
    private List<SelectItem.ValueBean> datas = new ArrayList<SelectItem.ValueBean>();

    @Override
    public int getRootViewId() {
        return R.layout.fragment_frame_filter;
    }

    @Override
    public void setViews() {
        setTitleTv("筛选");
        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(1,
                StaggeredGridLayoutManager.VERTICAL);
        layoutManager.setOrientation(StaggeredGridLayoutManager.VERTICAL);
        filterRecylerView.setLayoutManager(layoutManager);
        adapter = new FilterAdapter(getActivity(), null);
        filterRecylerView.setAdapter(adapter);
    }

    @Override
    public void initData() {
        Tools.showDialog(getActivity());
        NetUtils.getInstance().selectItems(new NetCallBack() {

            @Override
            public void onSuccess(String response, String mssage, ResultModel model) {
                Tools.dismissWaitDialog();
                ToastUtil.shortShowToast(mssage);
                list = ((List<SelectItem>) model.getModelList());
                if (list != null && list.size() > 0) {
                    adapter.appendAll(list);
                }
            }

            @Override
            public void onFail(boolean dataError, int errorCode, String mssage) {
                Tools.dismissWaitDialog();
                ToastUtil.shortShowToast(mssage);
            }
        }, SelectItem.class);
    }

    @Override
    public boolean mainTabShow() {
        return false;
    }


    @OnClick({R.id.filterReset, R.id.filterSure})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.filterReset:
                for (int i = 0; i < list.size(); i++) {
                    SelectItem selectItem = list.get(i);
                    List<SelectItem.ValueBean> value = selectItem.getValue();
                    for (int j = 0; j < value.size(); j++) {
                        SelectItem.ValueBean valueBean = value.get(j);
                        valueBean.setSelected(false);
                    }
                }
                adapter.appendAll(list);
                break;
            case R.id.filterSure:
                datas.clear();
                String result = "";
                for (int i = 0; i < list.size(); i++) {
                    SelectItem selectItem = list.get(i);
                    List<SelectItem.ValueBean> value = selectItem.getValue();
                    for (int j = 0; j < value.size(); j++) {
                        SelectItem.ValueBean valueBean = value.get(j);
                        if (valueBean.isSelected()) {
                            datas.add(valueBean);
                        }
                    }
                }
                for (int i = 0; i < datas.size(); i++) {
                    SelectItem.ValueBean valueBean = datas.get(i);
                    if (i == datas.size() - 1) {
                        result += valueBean.getId();
                    } else {
                        result += valueBean.getId() + ",";
                    }
                }
                BussEvent event = new BussEvent(BussEvent.Fiflter);
                event.setMessage(result);
                EventBus.getDefault().post(event);
                MyApplication.getInstance().manager.onBackPressed();
                break;
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.inject(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }
}
